<template>
    <Modal v-model="makeFileModal" :closable="false">
        <div slot="header">
            制作文件
            <Icon class="f-fr" type="ios-close" @click="closeModel"/>
        </div>
        <p style="font-size:14px;color:#383A4A;margin-bottom:8px;">
            <span style="color:#F45050">*</span>选择文件类型
        </p>
        <CheckboxGroup v-model="makeFileObj.types">
            <Checkbox label="NORMAL_DECLARE">
                <span>报关单</span>
            </Checkbox>
            <Checkbox label="CONTRACT">
                <span>合同</span>
            </Checkbox>
            <Checkbox label="INVOICE">
                <span>发票</span>
            </Checkbox>
            <Checkbox label="PACKING_lIST">
                <span>装箱单</span>
            </Checkbox>
        </CheckboxGroup>
        <p style="font-size:14px;color:#383A4A;margin-top:18px;margin-bottom:8px;">
            是否加盖电子签章
        </p>
        <p style="margin-bottom:20px;">
            <RadioGroup v-model="makeFileObj.isCachet">
                <Radio label="1">
                    <span>盖章</span>
                </Radio>
                <Radio label="0">
                    <span>不盖章</span>
                </Radio>
            </RadioGroup>
        </p>
        <Progress :percent="makeFilePercent" :class="{hide: isHide}"/>
        <div slot="footer">
            <Button type="default" @click="closeModel">取消</Button>
            <Button type="primary" @click="makeFileOk">开始制作</Button>
        </div>
    </Modal>
</template>

<script>
import { getToken } from '@/libs/util.js'
import { buildDoc } from '@/api/declare'
import { downloadURL } from '@/config/downloadUrl.js'

export default {
    name: 'MakefileModal',
    data () {
        return {
            makeFileModal: false,
            makeFileObj: { // 制作文件
                declareId: '',
                types: [],
                isCachet: ''
            },
            makeFilePercent: 0,
            isHide: false
        }
    },
    methods: {
        init (declareId) {
            this.makeFileModal = true
            this.makeFileObj.declareId = declareId
        },

        closeModel () {
            this.makeFileModal = false
        },

        makeFileOk () {
            if (this.makeFileObj.types.length === 0) {
                this.$Message.error('请选择要制作的文件')
                return false
            }

            this.isHide = false
            let data = {
                param: {
                    declareId: this.makeFileObj.declareId,
                    types: this.makeFileObj.types.join(','),
                    isCachet: this.makeFileObj.isCachet
                },
                Authorization: getToken()
            }
            buildDoc(data).then(res => {
                let param = encodeURI(JSON.stringify(data.param))
                window.open(downloadURL + '/ui/dataFileDownload/exportDeclareXls?param=' + param + '&Authorization=' + data.Authorization)
                this.makeFileModal = false
                this.isHide = true
            })
        }
    }
}
</script>

<style scoped>

</style>
